home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: cdf.toronto.edu!a228lave
- From: a228lave@cdf.toronto.edu (Laventure Derek)
- Subject: getchar() problems!
- Message-ID: <DL2wFD.8BK@cdf.toronto.edu>
- Sender: news@cdf.toronto.edu (Usenet News)
- Nntp-Posting-Host: sat
- Organization: University of Toronto Computing Disciplines Facility
- Date: Fri, 12 Jan 1996 17:25:59 GMT
-
- Hello!
-
- I am having some trouble with the following code. All I want it to do is
- read characters from the keyboard until the user types a # sign, at which point
- I want to stop accepting input, and tell the user some info. Here it is:
-
- #include <stdio.h>
-
- #define SPACE ' '
- #define NL '\n'
-
- int main(void)
- {
- char ch;
- int sp_c, nl_c, ot_c; /* Space count, Newline count and other count */
- sp_c=0;
- nl_c=0;
- ot_c=0;
-
- printf("Please enter a series of chars. Type '#' to quit.\n");
- while ((ch=getchar()) != '#')
- {
- switch (ch)
- {
- case SPACE: sp_c++; break;
- case NL : nl_c++; break;
- default : ot_c++;
- }
- }
- printf("There were %d spaces, %d newlines, and %d other
- chars!",sp_c,nl_c,ot_c);
- ch=getchar();
- }
-
- }
-
- What _actually_ happens is the code takes characters until you hit the #, and
- then continues until you press return. Also, the final ch=getchar(); line
- appears to be ignored... it's intended as a pause. I'm somewhat of a beginner
- to the language, so any help will be greatly appreciated!
-
- Thanx
-
- Derek Laventure
- a228lave@cdf.toronto.edu
-